package com.goldcn.user.service;

import com.goldcn.common.consts.RestConst;
import com.goldcn.common.exception.ServiceException;
import com.goldcn.common.util.CommonUtils;
import com.goldcn.common.util.IdGen;
import com.goldcn.interceptor.SystemContext;
import com.goldcn.user.dao.HotWordMapper;
import com.goldcn.user.model.HotWord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by 1111 on 2016/12/6.
 */
@Service
@Transactional
public class HotWordeService {
    @Autowired
    HotWordMapper hotWordMapper;

    public Object addOrUpdateWord(HotWord hotWord,int type){
        if(type==1){
            hotWord.setId(IdGen.get().nextId());
            hotWord.setAddTime(CommonUtils.getCurTimestamp());
            hotWord.setCompanyID(SystemContext.getCompanyID());
            hotWordMapper.insertSelective(hotWord);
            return "success";
        }
        if(type==2){
            hotWordMapper.updateByPrimaryKeySelective(hotWord);
            return "success";
        }
        throw new ServiceException("未知错误", RestConst.ErrorCode.UNKNOWN);
    }

    public Object deleteWord(Long id){
        hotWordMapper.deleteByPrimaryKey(id);
        return "success";
    }

    public Object selectAll(){
        return hotWordMapper.selectAll(SystemContext.getCompanyID());
    }
}
